'''
Created on 2011-3-6

@author: bnw
'''
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
from stocktable import stocklist

if __name__ == '__main__':
    for stock in stocklist:
        stockid = stock[0]
        urlstr = "http://money.finance.sina.com.cn/corp/go.php/vCI_StockStructure/stockid/" + stockid + ".phtml"
        page = urllib2.urlopen(urlstr)
        soup = BeautifulSoup(page)
        fpathstr = "F:/data/StockStructure/" + stockid + ".txt"
        f = open(fpathstr, 'w')
        tables = soup('table', id = re.compile('^StockStructureNewTable'))
        print "stock", stockid, tables.__len__()
        for table in tables:
            trs = table.tbody('tr')
            tds = trs[0]('td')
            for i in range(len(tds)):
                if i == 0:
                    continue
                f.write(trs[0]('td')[i].string)
                f.write('\t')
                f.write(trs[4]('td')[i].string)
                f.write('\t')
                f.write(trs[6]('td')[i].string)
                f.write('\t')
                f.write(trs[8]('td')[i].string)
                f.write('\n')               
        f.close()     
        
    
